package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投票记录表
 */
@BusinessDataConverter(
        insertSql = "insert into ND_REVIEW_RECORD (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, VOTERS, VOTING_RESULTS, APPROVAL_AMOUNT, RATING, REASON, PROCESS_ID, EXECUTION_ID, RATING_DESCRIPTION, PRO_ID, TASK_ID, VOTEBAT)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_REVIEW_RECORD SET CREATE_NAME =?,CREATE_BY =?,CREATE_DATE =?,UPDATE_NAME =?,UPDATE_BY =?,UPDATE_DATE =?,SYS_ORG_CODE =?,SYS_COMPANY_CODE =?,BPM_STATUS =?,VOTERS =?,VOTING_RESULTS =?,APPROVAL_AMOUNT =?,RATING =?,REASON =?,PROCESS_ID =?,EXECUTION_ID =?,RATING_DESCRIPTION =?,PRO_ID =?,TASK_ID =?,VOTEBAT =? WHERE ID = ?",
        findMigratedIdListSql = "SELECT ID FROM ND_REVIEW_RECORD WHERE PRO_ID = ?",
        seq = 4
)

public class NdReviewRecord extends AbstractBusiness{
    public NdReviewRecord(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {
        String btSessionAndVote = "SELECT bsev.*,bsam.MASKRESULT ,bsam.MASKDESC FROM BT_SESSION_EXPERT_VOTE bsev\n" +
                "LEFT JOIN BT_SESSION_AUDT_MASK bsam \n" +
                "ON bsev.PROCID  = bsam.PROCID AND bsev.TASKID  = bsam.TASKID AND bsev.VOTENO  = bsam.MASKNO \n" +
                "WHERE BSEV.ITEMNO = '"+this.projectId+"'";
        List<Map> btSessionAndVoteList = this.sourceDbUtil.selectByParams(btSessionAndVote, new ArrayList());
        for(Map btSessionAndVoteSig:btSessionAndVoteList){
            try {
                String sourceId = getSimpleId(btSessionAndVoteSig.get("CREATE_TIME").toString(), btSessionAndVoteSig.get("ITEMNO").toString());
                int needMigratedResult = this.needMigrated(sourceId);
                if (needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btSessionAndVoteSig,sourceId), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btSessionAndVoteSig,sourceId));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
                throw new Exception(String.format("投票记录表[%s]迁移失败:%s\n", btSessionAndVoteSig.get("ITEMNO"), e.getMessage()));
            }
        }


    }

    public List buildValues (Map row,String id) throws SQLException, ScriptException {
        List values = new ArrayList();
        String voters =BusinessTool.getEmpNameByEmpCode(row.get("VOTEEMPCODE"));
        //ID	主键
        values.add(id);
        //CREATE_NAME	创建人名称
        values.add(voters);
        //CREATE_BY	创建人登录名称
        values.add(row.get("VOTEEMPCODE"));
        //CREATE_DATE	创建日期
        values.add(row.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(row.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //VOTERS	投票人
        values.add(voters);
        //VOTING_RESULTS	投票结果 1:通过 2:不通过
        values.add(row.get("VOTERESULT"));
        //APPROVAL_AMOUNT	同意金额
        values.add(row.get("EXTNUM"));
        //RATING	报告评级
        values.add(row.get("MASKRESULT"));
        //REASON	备注（审核意见）
        values.add(Tool.replaceBrFlagStr(Tool.decodeUnicode(row.get("REMARK"))));
        //PROCESS_ID	流程id
        values.add(row.get("PROCID")+"-JW");
        //EXECUTION_ID	执行id
        values.add(null);
        //RATING_DESCRIPTION	评级说明
        values.add(row.get("MASKDESC"));
        //PRO_ID	项目ID
        values.add(row.get("ITEMNO"));
        //TASK_ID	节点ID
        values.add(row.get("TASKID")+"-JW");
        //VOTEBAT
        values.add(null);


        return values;
    }

}


